﻿package com.cenfun.ui {
	import flash.events.*;
	import fl.data.*;
	import fl.events.*;
	public final class TD extends DataProvider {
		public static const NODE_L:String = "node_l";
		public static const NODE_M:String = "node_m";
		//
		private var root_xml:XML;
		private var root:TN;
		private var copy_xml:XML;
		private var copy:TN;
		private var keyword:String = "";
		public function TD(xml:Object) {
			if (xml is XML) {
				root_xml = xml as XML;
				root = new TN(this, root_xml, null, 0);
				init();
			} else {
				throw new TypeError("Error: Invalid XML");
			}
		}
		public function init():void {
			keyword = "";
			data = [];
			root.open();
			invalidate();
		}
		public function find(kw:String):void {
			if (kw == keyword) {
				return;
			}
			keyword = kw;
			data = [];
			//
			copy_xml = new XML(<list/>);
			var xml:XML = search(root_xml);
			if (xml) {
				copy_xml = xml;
			}
			copy = new TN(this, copy_xml, null, 0);
			//
			copy.open();
			invalidate();
		}

		private function search(node:XML):XML {
			var xml:XML = node.copy();
			xml.setChildren("");
			xml.normalize();
			//子类含有
			if (node.hasComplexContent()) {
				for each (var item:XML in node.children()) {
					var child:XML = search(item);
					if (child) {
						xml.appendChild(child);
					}
				}
				if (xml.children().length()) {
					xml.@opened = "true";
					return xml;
				}
			}
			//自身含有
			var label:String = xml.@label;
			if (label) {
				var pattern:RegExp = new RegExp(keyword, "i");
				if (label.search(pattern) != -1) {
					return xml;
				}
			}
			return null;
		}
		
		
	}
}